export default {
  namespace: 'store',
  state: {
    male: [],
    female: [],
    count: 0
  },
  reducers: {
    addMale(state: { [propName: string]: any }, action: { [propName: string]: any }) {
      state.male = [...state.male, action.payload]
      state.count++
      return { ...state }
    },
    addFemale(state: { [propName: string]: any }, action: { [propName: string]: any }) {
      state.female = [...state.female, action.payload]
      state.count++
      return { ...state }
    },
    clearState() {
      return {
        male: [],
        female: [],
        count: 0
      }
    },
    showPerson(state: { [propName: string]: any }) {
      console.log('@', state.male);
      console.log('@@', state.female);
      return state
    }
  },
  effects: {
    *clear(_: any, { put, call, select }: any): Generator {
      yield call(() => {
        return new Promise((resolve, reject) => {
          setTimeout(resolve, 1000)
        })
      })
      //就是下面这句话报的错，原因是ts判断不出来*函数的返回类型，所以我们要给*函数加一个Generator返回类型限制
      const payload = yield select((state: any) => state.store.male)
      console.log('会被清除的男人', payload);
      yield put({
        type: 'clearState',
      })
    }
  },
  subscriptions: {
    setup({ dispatch, history }: any) {
      history.listen(({ pathname }: any) => {
        if (pathname === '/count') {
          dispatch({
            type: 'showPerson'
          })
        }
      })
    }
  }
}